<?php


namespace EasySwoole\EasySwoole;


use App\Process\HotReload;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\RedisPool\RedisPool;


class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');

        Di::getInstance()->set(SysConst::HTTP_CONTROLLER_MAX_DEPTH, 5);

        $dbConfig = \EasySwoole\EasySwoole\Config::getInstance()->getConf('MYSQL');

        /*******************************    MYSQL 链接池  **********************************************/
        $dbConfigObject = new \EasySwoole\ORM\Db\Config();
        $dbConfigObject->setHost($dbConfig['host']);
        $dbConfigObject->setPort($dbConfig['port']);
        $dbConfigObject->setUser($dbConfig['user']);
        $dbConfigObject->setPassword($dbConfig['password']);
        $dbConfigObject->setDatabase($dbConfig['database']);
        $dbConfigObject->setTimeout($dbConfig['timeout']);
        //连接池配置
        $dbConfigObject->setGetObjectTimeout($dbConfig['getObjectTimeout']); //设置获取连接池对象超时时间
        $dbConfigObject->setIntervalCheckTime($dbConfig['intervalCheckTime']); //设置检测连接存活执行回收和创建的周期
        $dbConfigObject->setMaxIdleTime($dbConfig['maxIdleTime']); //连接池对象最大闲置时间(秒)
        $dbConfigObject->setMinObjectNum($dbConfig['minObjectNum']); //设置最小连接池存在连接对象数量
        $dbConfigObject->setMaxObjectNum($dbConfig['maxObjectNum']); //设置最大连接池存在连接对象数量
        $dbConfigObject->setAutoPing($dbConfig['autoPing']); //设置自动ping客户端链接的间隔

        DbManager::getInstance()->addConnection(new Connection($dbConfigObject));


        /*******************************    REDIS 链接池  **********************************************/
        $redisConfig = \EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS');
        $redisConfigObject = new RedisConfig([
            'host' => $redisConfig['host'],
            'port' => $redisConfig['port'],
        ]);
        $redisPool = RedisPool::getInstance()->register($redisConfigObject);

        //配置连接池连接数
        $redisPool->setMinObjectNum($redisConfig['minObjectNum']);
        $redisPool->setMaxObjectNum($redisConfig['maxObjectNum']);
    }

    public static function mainServerCreate(EventRegister $register)
    {
        $register->add($register::onWorkerStart,function(){
            DbManager::getInstance()->getConnection()->__getClientPool()->keepMin(5);
        });

        /** * ****************   服务热重启    *****************/
        // 配置同上别忘了添加要检视的目录
        $hotReloadOptions = new \EasySwoole\HotReload\HotReloadOptions;
        $hotReload = new \EasySwoole\HotReload\HotReload($hotReloadOptions);
        $hotReloadOptions->setMonitorFolder([EASYSWOOLE_ROOT . '/App']);

        $server = ServerManager::getInstance()->getSwooleServer();
        $hotReload->attachToServer($server);
    }
}